package cn.iocoder.yudao.module.polling.service.pollingpath;

import cn.iocoder.yudao.module.polling.controller.admin.pollingpath.vo.PollingPathCreateReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingpath.vo.PollingPathExportReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingpath.vo.PollingPathPageReqVO;
import cn.iocoder.yudao.module.polling.controller.admin.pollingpath.vo.PollingPathUpdateReqVO;
import cn.iocoder.yudao.module.polling.convert.pollingpath.PollingPathConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingpath.PollingPathDO;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingpath.PollingPathMapper;
import cn.iocoder.yudao.module.polling.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 巡检路线 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class PollingPathServiceImpl implements PollingPathService {

    @Resource
    private PollingPathMapper pollingPathMapper;

    @Override
    public Integer createPollingPath(PollingPathCreateReqVO createReqVO) {
        // 插入
        PollingPathDO pollingPath = PollingPathConvert.INSTANCE.convert(createReqVO);
        pollingPathMapper.insert(pollingPath);
        // 返回
        return pollingPath.getId();
    }

    @Override
    public void updatePollingPath(PollingPathUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePollingPathExists(updateReqVO.getId());
        // 更新
        PollingPathDO updateObj = PollingPathConvert.INSTANCE.convert(updateReqVO);
        pollingPathMapper.updateById(updateObj);
    }

    @Override
    public void deletePollingPath(Integer id) {
        // 校验存在
        this.validatePollingPathExists(id);
        // 删除
        pollingPathMapper.deleteById(id);
    }

    private void validatePollingPathExists(Integer id) {
        if (pollingPathMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public PollingPathDO getPollingPath(Integer id) {
        return pollingPathMapper.selectById(id);
    }

    @Override
    public List<PollingPathDO> getPollingPathList(Collection<Integer> ids) {
        return pollingPathMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PollingPathDO> getPollingPathPage(PollingPathPageReqVO pageReqVO) {
        return pollingPathMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PollingPathDO> getPollingPathList(PollingPathExportReqVO exportReqVO) {
        return pollingPathMapper.selectList(exportReqVO);
    }

}
